package controllers

import (
	"github.com/beego/beego/v2/core/validation"
	beego "github.com/beego/beego/v2/server/web"
	"good/models"
	"math"
)

type GoodController struct {
	beego.Controller
}

func (c *GoodController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	name := c.GetString("name")
	price, _ := c.GetFloat("price", 64)
	weight, _ := c.GetInt("weight")
	num, _ := c.GetInt("num")
	times := c.GetString("times")

	var good = models.Good{
		Name:   name,
		Price:  price,
		Weight: weight,
		Num:    num,
		Times:  times,
	}
	valid := validation.Validation{}
	validation.SetDefaultMessage(map[string]string{
		"Required": "数据不能为空",
	})
	_, err := valid.Valid(&good)
	if err != nil {
		c.Data["json"] = models.JsonR{
			Code: 0,
			Msg:  "验证错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			c.Data["json"] = models.JsonR{
				Code: 0,
				Msg:  err.Error(),
				Data: nil,
			}
			c.ServeJSON()
		}
		return
	}

	err = models.DB.Create(&good).Error
	if err != nil {
		c.Data["json"] = models.JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.JsonR{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
}

func (c *GoodController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	page, _ := c.GetInt("page", 1)
	size := 5
	var count int64
	models.DB.Model(&models.Good{}).Count(&count)
	sumpage := int(math.Ceil(float64(count) / float64(size)))
	if page > sumpage {
		page = sumpage
	}
	offset := (page - 1) * size

	name := c.GetString("name")
	where := map[string]string{}
	if name != "" {
		where["name"] = name
	}
	var goods []models.Good
	err := models.DB.Where(where).Offset(offset).Limit(size).Find(&goods).Error
	if err != nil {
		c.Data["json"] = models.JsonR{
			Code: 0,
			Msg:  "展示失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	pagedate := map[string]interface{}{}
	pagedate["goods"] = goods
	pagedate["page"] = page
	pagedate["count"] = count
	pagedate["sumpage"] = sumpage
	pagedate["size"] = size

	c.Data["json"] = models.JsonR{
		Code: 200,
		Msg:  "展示成功",
		Data: pagedate,
	}
	c.ServeJSON()
}

func (c *GoodController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Good{}, id).Error
	if err != nil {
		c.Data["json"] = models.JsonR{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.JsonR{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *GoodController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	name := c.GetString("name")
	price, _ := c.GetFloat("price", 64)
	weight, _ := c.GetInt("weight")
	num, _ := c.GetInt("num")
	times := c.GetString("times")

	var good = models.Good{
		Id:     id,
		Name:   name,
		Price:  price,
		Weight: weight,
		Num:    num,
		Times:  times,
	}
	valid := validation.Validation{}
	validation.SetDefaultMessage(map[string]string{
		"Required": "数据不能为空",
	})
	_, err := valid.Valid(&good)
	if err != nil {
		c.Data["json"] = models.JsonR{
			Code: 0,
			Msg:  "验证错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			c.Data["json"] = models.JsonR{
				Code: 0,
				Msg:  err.Error(),
				Data: nil,
			}
			c.ServeJSON()
		}
		return
	}

	err = models.DB.Save(&good).Error
	if err != nil {
		c.Data["json"] = models.JsonR{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.JsonR{
		Code: 200,
		Msg:  "修改成功",
		Data: nil,
	}
	c.ServeJSON()
}
